import axios from '@/utils/request/axios';

export class GetUserListParams {
  pn: number;
  ps: number;
}

export class UserItem {
  id: number;
  comment: string;
  username: string;
  clearPasswd: string;
  roleIds: number[];
}

export async function getUserListNode(params: GetUserListParams) {
  const res = await axios({
    method: 'get',
    url: `/us/users`,
    params,
  });
  return {
    list: res.data.data.rows as UserItem[],
    total: res.data.data.total || 0,
  };
}

export async function getUserList(params: GetUserListParams) {
  const res = await axios({
    method: 'get',
    url: `/user/list`,
    params,
  });
  return {
    list: res.data.data as UserItem[],
    total: res.data.total || 0,
  };
}

export class AddUserNodeData {
  username: string;
  /** 用户的角色ID，2:用户;3:管理员 */
  roleIds: number[];
  comment: string;
}

export async function addUserNode(data: AddUserNodeData) {
  const res = await axios({
    method: 'post',
    url: '/us/users',
    data,
  });
  return res.data;
}

export class EditUserNodeData {
  username: string;
  /** 用户的角色ID，2:用户;3:管理员 */
  roleIds: number[];
  comment: string;
}

export async function editUserNode(id: number, data: EditUserNodeData) {
  const res = await axios({
    method: 'put',
    url: `/us/users/${id}`,
    data,
  });
  return res.data;
}

export class EditUserData {
  /** 添加时用0，编辑时用实际id */
  id: number;
  username: string;
  /** 用户的角色ID，2:用户;3:管理员 */
  role_id: number;
  comment: string;
}

export async function addUser(data: EditUserData) {
  const res = await axios({
    method: 'post',
    url: '/user/save',
    data,
  });
  return res.data;
}

export class DeleteUserData {
  /** 可批量，逗号分割 */
  ids: string;
}

export async function deleteUser(data: DeleteUserData) {
  const res = await axios({
    method: 'post',
    url: '/us/users/del',
    data,
  });
  return res.data;
}

export class ResetUserData {
  id: number;
  password: string;
}

export async function resetUser(data: ResetUserData) {
  const res = await axios({
    method: 'post',
    url: '/us/users/auth/resetPasswd',
    data,
  });
  return res.data;
}
